<?php
function markDir($dir,&$r)
{
    $fs = glob($dir.'/*');
    
    if(count($fs) == 0){
    	$r[] = $dir;
    }else{
    	foreach($fs as $f){
    		if(is_dir($f)){
    			markDir($f,$r);
    		}else{
    			$r[] = $f;
    		}
    	}
    }    
}
function mkdirs($dir, $mode = 0777)
{
    if (is_dir($dir) || @mkdir($dir, $mode)) return TRUE;
    if (!mkdirs(dirname($dir), $mode)) return FALSE;
 
    return @mkdir($dir, $mode);
}


function copyDir($from,$to){
	$res = array();
	markDir($from,$res);
	
	foreach($res as $f){
		$nf = str_replace($from,$to,$f);
		if(is_dir($f)){
			mkdirs($nf);
		}else{
			mkdirs(dirname($nf));
			copy($f,$nf);
		}	
	}
}
 function deldir($dir) {  
    //先删除目录下的文件：  
    $dh = opendir($dir);  
    while ($file = readdir($dh)) {  
        if($file != "." && $file!="..") {  
        $fullpath = $dir."/".$file;  
        if(!is_dir($fullpath)) {  
            unlink($fullpath);  
        } else {  
            deldir($fullpath);  
        }  
        }  
    }  
    closedir($dh);  
       
    //删除当前文件夹：  
    if(rmdir($dir)) {  
        return true;  
    } else {  
        return false;  
    }  
}

$currentPath = file_get_contents('../data/CurrentPath');
foreach($_POST['list'] as $file){
	if($file['type'] == 'folder'){
		copyDir($file['dirname'].'/'.$file['basename'],$currentPath.'/'.$file['basename']);
		if($_POST['method'] == 'cut'){
			deldir($file['dirname'].'/'.$file['basename']);
		}			
	}else{
		if($_POST['method'] == 'copy'){
			copy($file['dirname'].'/'.$file['basename'],$currentPath.'/'.$file['basename']);
		}else{
			rename($file['dirname'].'/'.$file['basename'],$currentPath.'/'.$file['basename']);
		}
	}
}
	
	
?>